package com.lzx.dormsys.service.impl;

import com.github.pagehelper.PageInfo;
import com.lzx.dormsys.bean.MenuBean;
import com.lzx.dormsys.bean.UserBean;
import com.lzx.dormsys.bean.UserBeanExample;
import com.lzx.dormsys.mapper.UserBeanMapper;
import com.lzx.dormsys.service.MenuService;
import com.lzx.dormsys.service.UserService;
import com.lzx.dormsys.util.MyMD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserBeanMapper userBeanMapper;

    @Autowired
    MenuService menuService;

    @Override
    public int insert(UserBean userBean) {
        userBean.setUserid(UUID.randomUUID().toString());
        String md5Pwd = new MyMD5().getMD5ofStr(userBean.getPwd()).toLowerCase();
        userBean.setPwd(md5Pwd);
        return userBeanMapper.insert(userBean);
    }

    @Override
    public int update(UserBean userBean) {
        int i = userBeanMapper.updateByPrimaryKey(userBean);
        return i;
    }

    @Override
    public List<UserBean> queryData(UserBean userBean) {
        UserBeanExample example = new UserBeanExample();
        UserBeanExample.Criteria criteria = example.createCriteria();
        if(null!=userBean.getGroupid()){
            criteria.andGroupidEqualTo(userBean.getGroupid());
        }
        if(null!=userBean.getSex()){
            criteria.andSexEqualTo(userBean.getSex());
        }
        if (null!=userBean.getLoginname()){
            criteria.andLoginnameLike("%"+userBean.getLoginname()+"%");
        }
        if(null!=userBean.getRealname()){
            criteria.andRealnameLike("%"+userBean.getRealname()+"%");
        }
        if(null!=userBean.getMobilephone()){
            System.out.println(userBean.getMobilephone());
            criteria.andMobilephoneLike("%"+userBean.getMobilephone()+"%");
        }
        if(null!=userBean.getStatus()){
            criteria.andStatusEqualTo(userBean.getStatus());
        }
        List<UserBean> list = userBeanMapper.selectByExample(example);
        return list;
    }



    @Override
    public PageInfo<UserBean> queryData(UserBean userBean, int pageSize, int pageNum) {
        return null;
    }

    @Override
    public int deleteByPrimaryKey(UserBean userBean) {
        return 0;
    }

    @Override
    public int deleteByPrimaryKey(String id) {
        return userBeanMapper.deleteByPrimaryKey(id);
    }

    @Override
    public UserBean login(UserBean userBean) {
        UserBeanExample example = new UserBeanExample();
        UserBeanExample.Criteria criteria = example.createCriteria();
        criteria.andLoginnameEqualTo(userBean.getLoginname());
        String md5Pwd = new MyMD5().getMD5ofStr(userBean.getPwd()).toLowerCase();
        criteria.andPwdEqualTo(md5Pwd);
        List<UserBean> list = userBeanMapper.selectByExample(example);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<Map<String, Object>> getLoginUserMenus(String userid) {

        /**
         * [
         *    {menuid:'m1',title:"主页",icon:"el-icon-location",children:[{menuid:'m11',title:"控制台",routerURL:"/home/wellcome"}]},
         *    {menuid:'m2',title:"系统设置",icon:"el-icon-location",children:[{menuid:'m22',title:"管理员列表",routerURL:"/home/userlist"}]},
         *    {menuid:'m4',title:"系统设置",icon:"el-icon-location",children:[{menuid:'m24',title:"管理员列表",routerURL:"/home/userlist"}]}
         * ]
         */

        List<MenuBean> menuBeanList = menuService.queryMenuByuserid(userid);
        //判断List<MenuBean> 中的paremntMenuid属性值是否为空，为空的做为一级菜单列表
        //不为空的parentMenuid就放到与之匹配的menuid对应children列表

        List<Map<String, Object>> returnValues = new ArrayList<>();

        for (MenuBean menuBean : menuBeanList) {
            if (menuBean.getParentmenuid() == null || menuBean.getParentmenuid().trim().equals("")) {
                Map<String, Object> level_1 = new HashMap<>();
                level_1.put("menuid", menuBean.getMenuid());
                level_1.put("title", menuBean.getTitle());
                level_1.put("icon", menuBean.getIcon());

                returnValues.add(level_1);

                List<Map<String, Object>> level2MapList = new ArrayList<>();
                for (MenuBean subMenuBean : menuBeanList) {//找与menuBean的menuid匹配的parentMenuid
                    if (subMenuBean.getParentmenuid() != null && subMenuBean.getParentmenuid().equals(menuBean.getMenuid())) {
                        Map<String, Object> level_2 = new HashMap<>();
                        level_2.put("routerURL", subMenuBean.getRouterurl());
                        level_2.put("menuid", subMenuBean.getMenuid());
                        level_2.put("title", subMenuBean.getTitle());
                        level2MapList.add(level_2);
                    }
                }
                level_1.put("children", level2MapList);
            }
        }
        return returnValues;
    }

    @Override
    public int setPassword(String userid, String newPasswod) {
        // 根据主键查询到当前用户
        UserBean userBean = userBeanMapper.selectByPrimaryKey(userid);
        // 修改密码
        userBean.setPwd(new MyMD5().getMD5ofStr(newPasswod).toLowerCase());
        // 更新用户并返回
        return userBeanMapper.updateByPrimaryKey(userBean);
    }

    @Override
    public UserBean getProfileByUserid(String userid) {
        return userBeanMapper.selectByPrimaryKey(userid);
    }
}
